package com.eleme.cart.controller;

import com.eleme.cart.dto.CartAddDTO;
import com.eleme.cart.dto.CartUpdateDTO;
import com.eleme.cart.service.CartService;
import com.eleme.cart.vo.CartItemVO;
import com.eleme.cart.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/CartController")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("/add")
    public Result<String> addItem(@Validated @RequestBody CartAddDTO dto) {
        return Result.success(cartService.addItem(dto));
    }

    @PostMapping("/update")
    public Result<String> updateItem(@Validated @RequestBody CartUpdateDTO dto) {
        return Result.success(cartService.updateItem(dto));
    }

    @DeleteMapping("/remove/{id}")
    public Result<String> removeItem(@PathVariable String id) {
        return Result.success(cartService.removeItem(id));
    }

    @PostMapping("/clear/{userId}")
    public Result<String> clearCart(@PathVariable String userId) {
        return Result.success(cartService.clearCart(userId));
    }

    @GetMapping("/listCart/{userId}")
    public Result<List<CartItemVO>> listCart(@PathVariable("userId") String userId) {
        return Result.success(cartService.listByUser(userId));
    }

    @GetMapping("/listCart/{userId}/{businessId}")
    public Result<List<CartItemVO>> listCart(@PathVariable("userId") String userId, @PathVariable("businessId") Integer businessId) {
        return Result.success(cartService.listByUser(userId));
    }

    @PostMapping("/saveCart/{userId}/{businessId}/{foodId}")
    public Result<Integer> saveCart(@PathVariable("userId") String userId, @PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId) {
        return Result.success(cartService.saveCart(userId, businessId, foodId));
    }

    @PutMapping("/updateCart/{userId}/{businessId}/{foodId}/{quantity}")
    public Result<Integer> updateCart(@PathVariable("userId") String userId, @PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId, @PathVariable("quantity") Integer quantity) {
        return Result.success(cartService.updateCart(userId, businessId, foodId, quantity));
    }

    @DeleteMapping("/removeCart/{userId}/{businessId}/{foodId}")
    public Result<Integer> removeCart(@PathVariable("userId") String userId, @PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId) {
        return Result.success(cartService.removeCart(userId, businessId, foodId));
    }
} 